package com.cy.pj.sys.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cy.pj.common.vo.PageResult;
import com.cy.pj.exception.ServiceException;
import com.cy.pj.sys.dao.SysMemberDao;
import com.cy.pj.sys.entity.SysMember;
import com.cy.pj.sys.service.SysMemberService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SysMemberServiceImpl implements SysMemberService{

	@Autowired
	private SysMemberDao sysMemberDao;

	@Override
	public PageResult<SysMember> findPageObjects(String memberName, Integer pageCurrent) {
		// 校验参数
		if(pageCurrent==0 || pageCurrent<1)
			throw new IllegalArgumentException("请输入正确的页码值");

		// 查询总数
		int rowCount = sysMemberDao.getRowCount(memberName);
		if(rowCount==0)
			throw new ServiceException("没有用户信息");

		// 分页查询
		int pageSize = 3;
		int startIndex = (pageCurrent-1)*pageSize;
		List<SysMember> records=
				sysMemberDao.findPageObjects(memberName, startIndex, pageSize);

		// 封装
		PageResult<SysMember> list = new PageResult<>();
		list.setPageCount( (rowCount-1)/pageSize+1 );
		list.setPageCurrent(pageCurrent);
		list.setPageSize(pageSize);
		list.setRecords(records);
		list.setRowCount(rowCount);

		return list;
	}

	@Override
	public int saveMemberInfo(SysMember sysMember) {
		long start=System.currentTimeMillis();
		log.info("start:"+start);
		//1.参数校验
		if(sysMember==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(sysMember.getMemberName()))
			throw new ServiceException("会员名不能为空");
		if(StringUtils.isEmpty(sysMember.getPassword()))
			throw new ServiceException("密码不能为空");
		
		//2.保存用户自身信息
		//2.1对密码进行加密
		String source=sysMember.getPassword();
		String salt=UUID.randomUUID().toString();
		SimpleHash sh=new SimpleHash(//Shiro框架
				"MD5",//algorithmName 算法
				source,//原密码
				salt, //盐值
				1);//hashIterations表示加密次数
		sysMember.setSalt(salt);
		sysMember.setPassword(sh.toHex());
		//3.保存用户角色关系数据
		int rows = sysMemberDao.insertMemberInfor(sysMember);
		
		long end=System.currentTimeMillis();
		log.info("end:"+end);
		log.info("total time :"+(end-start));
		//4.返回结果
		return rows;
	}



	@Override
	public SysMember findMemberByMobile(String mobile) {
		return sysMemberDao.selectMemberByMobile(mobile);
	}

	@Override
	public int updateAccountByMobile(Long account, Long money, String mobile) {
		if(StringUtils.isEmpty(account) || account==null)
			throw new IllegalArgumentException("请输入充值金额");


		Long newAccount = account+money;
		int rows = sysMemberDao.updateAccountByMobile(newAccount , mobile);
		return rows;


	}

	/**
	 * 基于id修改会员信息
	 */
	@Override
	public int updateMember(SysMember entity) {
		sysMemberDao.updateMemberById(entity);

		return 0;
	}

	@Override
	public int deleteByMobile(String[] mobiles) {

		int rows = sysMemberDao.deleteByMobile(mobiles);
		return rows;
	}


}











